<style scoped>
</style>
<!--  -->
<template>
  <div>
    <el-table v-loading="loading" :data="pagedata" style="width: 100%">
      <el-table-column prop="name" label="页面名称">
        <template slot-scope="scope">
          <i :class="'noselect icon iconfont'" :style="'margin-left: '+rowMaringLeft(scope.row.index)+'px'"></i>
          <span style="margin-left: 10px">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="type" label="类型" align="center" width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.type === 1">普通页面</span>
          <span v-if="scope.row.type === 2" class="green">系统页面</span>
          <span v-if="scope.row.type === 3" class="orange">外链接页面</span>
          <span v-if="scope.row.type === 4" class="orange">内链接页面</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="230" align="center">
        <template slot-scope="scope">
          <el-button @click="editorPageSeo(scope.row.id)" type="text" size="small">编辑页面SEO信息</el-button>
        </template>
      </el-table-column>
    </el-table>
    <SeoPageDialog ref="seoPageDialog" />
  </div>
</template>

<script>
import SeoPageDialog from '@/components/main/user/siteseo/common/SeoPageDialog.vue'
export default {
  created () {
    this.initPageData()
  },
  data () {
    return {
      pagedata: [],
      loading: false
    }
  },
  methods: {
    initPageData () {
      this.loading = true
      this.$post('/page/infos', null).then(response => {
        if (response && response.total) {
          this.pagedata = initResponsePageData(response.pages)
        } else {
          this.pagedata = []
        }
        this.loading = false
      })
    },
    rowMaringLeft (index) {
      if (index === 0) {
        return 0
      }
      let result = 15
      for (let i = 0; i < index; i++) {
        result += 15
      }
      return result
    },
    editorPageSeo (id) {
      this.$refs['seoPageDialog'].show(id)
    }
  },
  components: {
    SeoPageDialog
  }
}

function initResponsePageData (data) {
  if (!data) {
    return []
  }
  let result = []
  let topData = data[0]
  for (let i in topData) {
    let topItem = topData[i]
    result.push({
      index: 0,
      id: topItem.id,
      parentId: topItem.parentId,
      name: topItem.name,
      type: topItem.type,
      status: topItem.status,
      orderNum: topItem.orderNum
    })
    for (let j in data[topItem.id]) {
      let item = data[topItem.id][j]
      result.push({
        index: 1,
        id: item.id,
        parentId: item.parentId,
        name: item.name,
        type: item.type,
        status: item.status,
        orderNum: item.orderNum
      })
      for (let k in data[item.id]) {
        let ite = data[item.id][k]
        result.push({
          index: 2,
          id: ite.id,
          parentId: ite.parentId,
          name: ite.name,
          type: ite.type,
          status: ite.status,
          orderNum: ite.orderNum
        })
      }
    }
  }
  return result
}

</script>
